<?php
/**
 * @author Karel Wesseling <karel@swc.nl>
 * @version 1.0
 * @copyright (c) 2013, Karel Wesseling
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of 
 * this software and associated documentation files (the "Software"), to deal in the 
 * Software without restriction, including without limitation the rights to use, copy, 
 * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, subject to the 
 * following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * Client to access the Strava-api.
 * @see http://strava.github.io/api/
 */
class Strava {
	
	const ENDPOINT = 'https://www.strava.com/api';
		
	/**
	 * The api key token for the requested account. Your accountmanager can 
	 * provide you this key.
	 * @var string  
	 */
	public $token;
	
	/**
	 * @param string $api_key_token The api key token for the requested account
	 */
	public function __construct($api_key_token) {
		$this->token = $api_key_token;
	}
	
	/**
	 * Create a GET call to the REST-api
	 * @param array $params
	 * @return StdClass Result of the call
	 */
	protected function get($endpoint, array $params = array()) {
		$params = array_merge($params, array(
			'access_token'		=> $this->token,
		));
		$url = self::ENDPOINT.$endpoint.'?'.http_build_query($params);
		$ch = curl_init($url);
		curl_setopt_array($ch, array(
			CURLOPT_RETURNTRANSFER	=> true,
			CURLOPT_SSL_VERIFYPEER	=> false,
		));
		$res = curl_exec($ch);
		curl_close($ch);
		return json_decode($res);
	}

	/**
	 * Create a POST call to the REST-api
	 * @param array $params
	 * @return StdClass Result of the call
	 */
	protected function post($endpoint, array $params = array()) {
		$params = array_merge($params, array(
			'access_token'		=> $this->token,
		));
		$url = self::ENDPOINT.$endpoint;
		$ch = curl_init($url);
		curl_setopt_array($ch, array(
			CURLOPT_RETURNTRANSFER	=> true,
			CURLOPT_SSL_VERIFYPEER	=> false,
			CURLOPT_POST			=> true,
			CURLOPT_POSTFIELDS		=> $params,
		));
		$res = curl_exec($ch);
		curl_close($ch);
		return json_decode($res);
	}
	
	/**
	 * Call the athlete api endpoint
	 * @param int $id
	 * @return json Current athletes details or public profile of other athlete
	 * when id not null.
	 */
	public function athlete($id = null) {
		return $this->get('/v3/athlete'.($id === null ? '':'/'.$id));
	}
	
	/**
	 * Check the status of an upload.
	 * @param int $id
	 * @return json The status
	 */
	public function uploadstatus($id) {
		return $this->get('/v3/uploads/'.$id);
	}
	
	/**
	 * Upload a file to Strava.
	 * @param string $file_path
	 * @param string $data_type fit,gpx,etc...
	 * @param string $activity_name
	 * @param string $activity_type
	 * @return json Result of the upload or null on errors
	 */
	public function upload($file_path, $data_type = 'fit', $activity_name = null, $activity_type = null) {
		$params = array(
			'file'			=> '@'.$file_path,
			'data_type'		=> $data_type,
		);
		if ($activity_name !== null) {
			$params['activity_name'] = (string)$activity_name;
		}
		if ($activity_type !== null) {
			$params['activity_type'] = (string)$activity_type;
		}
		return $this->post('/v3/uploads', $params);
	}

}